Online-Academy
Look, Read, Understand, Apply

PHP

Q and A - II

  1. How do you retrieve a cookie value?

    echo $_COOKIE['user'];

  2. What is the difference between $_GET and $_POST?

    $_GET sends data via the URL, $_POST sends it via HTTP body (more secure).

  3. What is the $_SERVER superglobal?

    Contains information about headers, paths, and script locations.

  4. What does die() or exit() do?

    Terminates script execution immediately.

  5. How do you redirect to another page in PHP?

    • header("Location: home.php");
    • exit();

  6. How do you handle file uploads in PHP?

    Using $_FILES superglobal and move_uploaded_file() function.

  7. How do you open and read a file in PHP?
        $file = fopen("data.txt", "r");
        echo fread($file, filesize("data.txt"));
        fclose($file);
    

  8. What is error handling in PHP?

    Managing runtime errors using try, catch, and throw.

  9. How do you define a function in PHP?
        function greet($name) {
            return "Hello, $name!";
        }
    
    
  10. What is the scope of variables in PHP?

    Local, Global, Static, and Parameter scope.

  11. What is Object-Oriented Programming (OOP) in PHP?

    A method of programming that uses objects and classes to organize code.

  12. How do you define a class and object in PHP?
    class Person {
        public $name;
        function sayHello() {
            echo "Hello!";
        }
    }
    $person = new Person();
    $person->sayHello();
    
  13. What are PHP access modifiers?

    public, protected, and private.

  14. What is inheritance in PHP?

    When a class inherits properties and methods from another class using the extends keyword.

  15. What is an interface in PHP?

    An interface defines methods that must be implemented by a class.

  16. What is a trait in PHP?

    Traits allow code reuse across classes without inheritance.

  17. How do you handle exceptions in PHP?
    try {
       throw new Exception("Error occurred");
    } catch (Exception $e) {
       echo $e->getMessage();
    }
    
    
  18. What is the difference between include_once and require_once?

    They work like include and require, but prevent multiple inclusions of the same file.

  19. How do you encrypt and decrypt data in PHP?

    Using password_hash() and password_verify() for passwords.

  20. How do you send an email in PHP?

    mail("user@example.com", "Subject", "Message", "From: admin@example.com");

  21. What are magic methods in PHP?

    Special methods starting with __ like __construct(), __destruct(), __get(), etc.

  22. What is Composer in PHP?

    A dependency manager for installing and managing PHP libraries.

  23. What is Laravel?

    A PHP web framework that follows the MVC (Model-View-Controller) architecture.

  24. What is the difference between == and strcmp() when comparing strings?

    == is case-insensitive in type juggling; strcmp() is case-sensitive and returns integer values.

  25. How can you prevent SQL Injection in PHP?

    Use prepared statements or parameterized queries via PDO or MySQLi.